// DVBTableDialog.cpp : implementation file
//

#include "stdafx.h"
#include "TSG.h"
#include "DVBTableDialog.h"
#include "MainFrm.h"
#include "TSGDoc.h"

#include "NewTableDlg.h"


// CDVBTableDialog dialog

IMPLEMENT_DYNAMIC(CDVBTableDialog, CDialog)

CDVBTableDialog::CDVBTableDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CDVBTableDialog::IDD, pParent)
{

}

CDVBTableDialog::~CDVBTableDialog()
{
}

void CDVBTableDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_TABLE, m_TableList);
	DDX_Control(pDX, IDC_TREE_TABLE, m_TableTree);
}


BEGIN_MESSAGE_MAP(CDVBTableDialog, CDialog)
	ON_WM_CTLCOLOR()
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BUT_NEW_TABLE, &CDVBTableDialog::OnBnClickedButNewTable)
	ON_BN_CLICKED(IDC_BUT_TABLE_APPLY, &CDVBTableDialog::OnBnClickedButTableApply)
	ON_LBN_SELCHANGE(IDC_LIST_TABLE, &CDVBTableDialog::OnLbnSelchangeListTable)
END_MESSAGE_MAP()


// CDVBTableDialog message handlers

HBRUSH CDVBTableDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  Change any attributes of the DC here
	if(nCtlColor == CTLCOLOR_DLG || nCtlColor == CTLCOLOR_STATIC)
	{
		hbr = CreateSolidBrush(RGB(255,255,255));
	}

	// TODO:  Return a different brush if the default is not desired
	return hbr;
}



void CDVBTableDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here	
	CRect rc;
	
	if(GetDlgItem(IDC_TREE_TABLE))
	{	
		GetClientRect(&rc);
		rc.left += rc.Width()/4 + 5;
		rc.top += 10;
		rc.right -= 5;
		rc.bottom -= 30;
		GetDlgItem(IDC_TREE_TABLE)->MoveWindow(rc);
	}
	if(GetDlgItem(IDC_LIST_TABLE))
	{
		GetClientRect(&rc);
		rc.left += 5;
		rc.top	+= 10;
		rc.right = rc.Width()/4 - 5;
		rc.bottom -= 30;
		GetDlgItem(IDC_LIST_TABLE)->MoveWindow(rc);
	}
	if(GetDlgItem(IDC_BUT_TABLE_APPLY))
	{
		CRect rcBut;
		GetClientRect(&rc);
		GetDlgItem(IDC_BUT_TABLE_APPLY)->GetClientRect(&rcBut);
		rc.left = rc.right - 100;
		rc.top = rc.bottom - 25;
		rc.right = rc.left+rcBut.Width();
		rc.bottom = rc.top + rcBut.Height();
		GetDlgItem(IDC_BUT_TABLE_APPLY)->MoveWindow(rc);
	}
	if(GetDlgItem(IDC_BUT_NEW_TABLE))
	{
		CRect rcBut;
		GetClientRect(&rc);
		GetDlgItem(IDC_BUT_NEW_TABLE)->GetClientRect(&rcBut);
		rc.left = rc.right - 200;
		rc.top = rc.bottom - 25;
		rc.right = rc.left+rcBut.Width();
		rc.bottom = rc.top + rcBut.Height();
		GetDlgItem(IDC_BUT_NEW_TABLE)->MoveWindow(rc);
	}
}

BOOL CDVBTableDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	CWnd* pBtnApply = GetDlgItem(IDC_BUT_TABLE_APPLY);
	if(pBtnApply)
	{
		pBtnApply->EnableWindow(FALSE);
	}


	m_TableTree.ModifyStyle(0, TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS);
	CTSGDoc * pCurrentDoc =(CTSGDoc*)((CMainFrame *)AfxGetMainWnd())->GetActiveDocument();

	int nTableCount = pCurrentDoc->GetDVBTableCount();
	for(int n = 0; n < nTableCount; n++)
	{
		CString str;
		str = "Table: ";
		char* pTableName = pCurrentDoc->GetDVBTableName(n);
		str += pTableName;
		m_TableList.InsertString(n, str);	
	}

	m_TableList.SetCurSel(0);

	DrawTable();
		
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

static HTREEITEM g_TreeNodeStack[8] = {0};
static HTREEITEM* g_TreeStackTop = g_TreeNodeStack;
static HTREEITEM* g_TreeStackButtom = g_TreeNodeStack;
static void pushTreeNode(HTREEITEM hItem)
{
	if(g_TreeStackTop < g_TreeNodeStack + 8)
	{
		*g_TreeStackTop = hItem;
		g_TreeStackTop++;
	}
}
static HTREEITEM popTreeNode(void)
{
	HTREEITEM hItem = NULL;
	if(g_TreeStackTop > g_TreeNodeStack)
	{
		g_TreeStackTop--;
		hItem = *g_TreeStackTop;
		*g_TreeStackTop = NULL;
	}

	return hItem;
}

BOOL CDVBTableDialog::DrawTable(void)
{
	int nIndex = m_TableList.GetCurSel();
	int nChildNodeIndex = 1;
	BYTE bPosition = 2;
	char pLine[128] = {0};
	BOOL bNext = FALSE;
	HTREEITEM hParent = NULL;
	HTREEITEM hItem = NULL;
	CTSGDoc * pCurrentDoc =(CTSGDoc*)((CMainFrame *)AfxGetMainWnd())->GetActiveDocument();

	m_TableTree.DeleteAllItems();

	bNext = pCurrentDoc->ParseDVBTable(nIndex, NULL, pLine, bPosition);
	while(bNext)
	{
		//bPosition: 1-Upper, 2-current, 3-Lower
		CString str;
		str = pLine;		

		if(str.GetLength()>0)
		{
			TVINSERTSTRUCT tvInsert;
			tvInsert.hParent = hParent;
			tvInsert.hInsertAfter = NULL;
			tvInsert.item.mask = TVIF_TEXT;
			tvInsert.item.pszText = str.AllocSysString();
			hItem = m_TableTree.InsertItem(&tvInsert);
			::SysFreeString(tvInsert.item.pszText);
			m_TableTree.EnsureVisible(hItem);
		}

		if(bPosition == 1)
		{
			hParent = popTreeNode();
		}

		if(bPosition == 3)
		{
			pushTreeNode(hParent);
			hParent = hItem;			
		}		

		bPosition = 2;
		bNext = pCurrentDoc->ParseDVBTable(nIndex, NULL, pLine, bPosition);		
	}
	
	return TRUE;
}

void CDVBTableDialog::OnBnClickedButNewTable()
{
	// TODO: Add your control notification handler code here
	CNewTableDlg dlg;
	CTSGDoc * pCurrentDoc =(CTSGDoc*)((CMainFrame *)AfxGetMainWnd())->GetActiveDocument();
	if(IDOK == dlg.DoModal())
	{
		CWnd* pBtnApply = GetDlgItem(IDC_BUT_TABLE_APPLY);
		if(pBtnApply)
		{
			pBtnApply->EnableWindow(TRUE);
		}
		
		m_TableList.ResetContent();
		m_TableTree.DeleteAllItems();

		int nTableCount = pCurrentDoc->GetDVBTableCount();
		for(int n = 0; n < nTableCount; n++)
		{
			CString str;
			str = "Table: ";
			char* pTableName = pCurrentDoc->GetDVBTableName(n);
			str += pTableName;
			m_TableList.InsertString(n, str);	
		}

		m_TableList.SetCurSel(0);

		DrawTable();
	}
	
}

void CDVBTableDialog::OnBnClickedButTableApply()
{
	// TODO: Add your control notification handler code here
	CTSGDoc * pCurrentDoc =(CTSGDoc*)((CMainFrame *)AfxGetMainWnd())->GetActiveDocument();
	pCurrentDoc->RebuildXML();

	CWnd* pBtnApply = GetDlgItem(IDC_BUT_TABLE_APPLY);
	if(pBtnApply)
	{
		pBtnApply->EnableWindow(FALSE);
	}
}

void CDVBTableDialog::OnLbnSelchangeListTable()
{
	// TODO: Add your control notification handler code here
	m_TableTree.DeleteAllItems();	

	DrawTable();
}
